/*     */ package com.zimbra.cs.mailbox;
/*     */ 
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.mime.Mime;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStream;
/*     */ import java.io.OutputStream;
/*     */ import javax.activation.DataSource;
/*     */ import javax.mail.MessagingException;
/*     */ import javax.mail.internet.MimeMessage;
/*     */ import javax.mail.internet.MimePart;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class AttachmentDataSource
/*     */   implements DataSource
/*     */ {
/*     */   private Contact mContact;
/*     */   private String mPartName;
/*     */   
/*     */   public AttachmentDataSource(Contact contact, String partName)
/*     */   {
/*  38 */     if (contact == null) {
/*  39 */       throw new NullPointerException("contact cannot be null");
/*     */     }
/*  41 */     if (partName == null) {
/*  42 */       throw new NullPointerException("partName cannot be null");
/*     */     }
/*  44 */     this.mContact = contact;
/*  45 */     this.mPartName = partName;
/*     */   }
/*     */   
/*     */   public String getContentType() {
/*  49 */     String contentType = null;
/*  50 */     MimePart mp = null;
/*     */     try {
/*  52 */       mp = getMimePart();
/*  53 */       if (mp != null) {
/*  54 */         contentType = mp.getContentType();
/*     */       }
/*     */     } catch (Exception e) {
/*  57 */       ZimbraLog.mailbox.error("Unable to determine content type for contact %d.", Integer.valueOf(this.mContact.getId()), e);
/*     */     }
/*  59 */     return contentType;
/*     */   }
/*     */   
/*     */   private MimePart getMimePart() throws MessagingException, ServiceException
/*     */   {
/*  64 */     MimeMessage msg = this.mContact.getMimeMessage(false);
/*  65 */     MimePart mp = null;
/*     */     try {
/*  67 */       mp = Mime.getMimePart(msg, this.mPartName);
/*     */     } catch (IOException e) {
/*  69 */       throw ServiceException.FAILURE("Unable to look up part " + this.mPartName + " for contact " + this.mContact.getId(), null);
/*     */     }
/*     */     
/*  72 */     if (mp == null) {
/*  73 */       ZimbraLog.mailbox.warn("Unable to find part %s for contact %d.", new Object[] { this.mPartName, Integer.valueOf(this.mContact.getId()) });
/*     */     }
/*  75 */     return mp;
/*     */   }
/*     */   
/*     */   public InputStream getInputStream() throws IOException {
/*     */     try {
/*  80 */       return getMimePart().getInputStream();
/*     */     } catch (Exception e) {
/*  82 */       ZimbraLog.mailbox.error("Unable to get stream to part %s for contact %d.", new Object[] { this.mPartName, Integer.valueOf(this.mContact.getId()) });
/*  83 */       throw new IOException(e.toString());
/*     */     }
/*     */   }
/*     */   
/*     */   public String getName() {
/*  88 */     MimePart mp = null;
/*  89 */     String name = null;
/*     */     try {
/*  91 */       mp = getMimePart();
/*  92 */       if (mp != null) {
/*  93 */         name = mp.getFileName();
/*     */       }
/*     */     } catch (Exception e) {
/*  96 */       ZimbraLog.mailbox.error("Unable to determine the filename for contact %d, part %s.", Integer.valueOf(this.mContact.getId()), this.mPartName, e);
/*     */     }
/*  98 */     return name;
/*     */   }
/*     */   
/*     */   public OutputStream getOutputStream() {
/* 102 */     throw new UnsupportedOperationException();
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/mailbox/AttachmentDataSource.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */